package com.example.admin.mycustomview.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by admin on 2016/6/28.
 * 画布的缩放
 */
public class LayerViewFive extends View {

    private Paint mPaint;
    private int mWidth, mHigth;

    public LayerViewFive(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // 将画笔设置为空心
        mPaint.setStyle(Paint.Style.STROKE);
        // 设置画笔颜色
        mPaint.setColor(Color.BLACK);
        // 设置画笔宽度
        mPaint.setStrokeWidth((float) 2.0);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mHigth = w;
        mWidth = w;
    }

    @Override
    protected void onDraw(Canvas canvas) {

        for (int i = 0; i < 20; i++) {
            // 保存画布
            canvas.save();
            float fraction = (float) i / 20;
            // 将画布以正方形中心进行缩放
            canvas.scale(fraction, fraction, mHigth / 2, mWidth / 2);
            canvas.drawRect(new Rect(0, 0, mWidth, mWidth), mPaint);
            // 画布回滚
            canvas.restore();
        }
    }
}
